﻿using System;
using System.IO;

namespace zevenseas.Community.Utilities
{
    class StapleBuilder
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 5)
                {
                    Usage();
                    return;
                }

                if (args[3] != "WebApplication" && args[3] != "Farm")
                {
                    Usage();
                    return;
                }

                string StapleTitle = args[0];
                string SiteDefinitions = args[1];
                string FeatureGuid = args[2];
                string Scope = args[3];
                string FeatureFolder = args[4];

                CreateFeature(Scope, StapleTitle, FeatureFolder);

                char[] splitter = {';', ','};
                string[] AllSiteDefinitions = SiteDefinitions.Split(splitter, StringSplitOptions.RemoveEmptyEntries);

                FileInfo f = new FileInfo(FeatureFolder + @"\\basesitestapling.xml");
                StreamWriter w;
                w = f.CreateText();
                w.WriteLine(@"<Elements xmlns=""http://schemas.microsoft.com/sharepoint/"">");

                foreach (string SiteDefinition in AllSiteDefinitions)
                {
                    CreateTemplateAssociation(w, FeatureGuid, SiteDefinition);
                }

                w.WriteLine(@"</Elements>");
                w.Close();
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
                Usage();
            }
        }

        private static void CreateTemplateAssociation(StreamWriter w, string FeatureGuid, string SiteDefinition)
        {
            w.WriteLine(@"  <FeatureSiteTemplateAssociation Id=""" + FeatureGuid + @""" TemplateName=""" + SiteDefinition + @"""  />");
        }

        private static void CreateFeature(string FeatureScope, string StapleTitle, string FeatureFolder)
        {
            Directory.CreateDirectory(FeatureFolder);

            FileInfo f = new FileInfo(FeatureFolder + @"\\Feature.xml");
            StreamWriter w = f.CreateText();

            w.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8"" ?>");
            w.WriteLine(@"<Feature Id=""" + Guid.NewGuid() + @"""");
            w.WriteLine(@"  Title=""Base Site Staple: " + StapleTitle + @"""");
            w.WriteLine(@"  Description=""A Base Site Staple Titled: " + StapleTitle + @"""");
            w.WriteLine(@"  Version=""1.0.0.0""");
            w.WriteLine(@"  Scope=""" + FeatureScope + @"""");
            w.WriteLine(@"  Hidden=""FALSE""");
            w.WriteLine(@"  xmlns=""http://schemas.microsoft.com/sharepoint/"">");
            w.WriteLine(@"  <ElementManifests>");
            w.WriteLine(@"      <ElementManifest Location=""basesitestapling.xml""/>");
            w.WriteLine(@"  </ElementManifests>");
            w.WriteLine(@"</Feature>");
            w.Close();
        }

        static void Usage()
        {
            Console.WriteLine("");
            Console.WriteLine("Base Site Staple Feature Generator");
            Console.WriteLine("-------------------------------");
            Console.WriteLine("Usage: zsStapleGenerator <StapleTitle> <SiteDefinitions> <FeatureGuid> <Scope> <FeatureFolder>");
            Console.WriteLine("");
            Console.WriteLine("<StapleTitle>      Name you would like to give the new StapleFeature");
            Console.WriteLine("<SiteDefinitions>  List of SiteDefinitions to be stapled (';' or ',' delimited");
            Console.WriteLine("<FeatureGuid>      The Guid of the Feature that is to be stapled");
            Console.WriteLine("<Scope>            MUST be either 'WebApplication' or 'Farm'");
            Console.WriteLine("<FeatureFolder>    The New folder where you would like to create the feature.");
            Console.WriteLine("");
            Console.WriteLine("");
        }
    }
}
